﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Caching;

public class db
{
    class ayarlar
    {
        public static string cs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["baglanti"].ToString();
    }

    public static string Insert(string TabloAdi, object Class, bool Identy)
    {
        string strGeri = string.Empty;
        try
        {
            using (SqlConnection conn = new SqlConnection(ayarlar.cs))
            {
                string strTemp = string.Empty;
                string strAlanAdi = string.Empty;
                string strVeri=string.Empty;
                SqlCommand sql = new SqlCommand();
                sql.Connection = conn;
                foreach (var item in Class.GetType().GetProperties())
                {
                    strVeri = item.GetValue(Class, item.GetIndexParameters()).ToString();
                    if (item.Name != "Cihaz_ID")
                    {
                        if (item.PropertyType.Name == "DateTime")
                        {
                            sql.Parameters.Add("@" + item.Name, SqlDbType.DateTime).Value = strVeri;
                        }
                        else if (item.PropertyType.Name == "BigInt")
                        {
                            sql.Parameters.Add("@" + item.Name, SqlDbType.BigInt).Value = strVeri;
                        }
                        else
                        {
                            sql.Parameters.Add("@" + item.Name, SqlDbType.NVarChar).Value = strVeri;
                        }
                        strTemp += "@" + item.Name + ",";
                        strAlanAdi += item.Name + ",";
                    }
                    else
                    {
                        sql.Parameters.Add("@" + item.Name, SqlDbType.BigInt).Value = strVeri;
                        strTemp += "@" + item.Name + ",";
                        strAlanAdi += item.Name + ",";
                    }
                }
                strTemp = strTemp.Substring(0, strTemp.Length - 1);
                strAlanAdi = strAlanAdi.Substring(0, strAlanAdi.Length - 1);
                conn.Open();
                if (Identy)
                {
                    sql.CommandText = "Insert Into " + TabloAdi + " (" + strAlanAdi + ") values (" + strTemp + ") SELECT @@IDENTITY";
                    strGeri = sql.ExecuteScalar().ToString();
                }
                else
                {
                    sql.CommandText = "Insert Into " + TabloAdi + " (" + strAlanAdi + ") values (" + strTemp + ")";
                    sql.ExecuteNonQuery();
                    strGeri = null;
                }
                sql.Dispose();
                conn.Close();
                return strGeri;
            }
        }
        catch (Exception h)
        {
            throw new Exception(h.Message);
        }
    }
    public static void Delete(string TabloAdi, string Where)
    {
        using (SqlConnection conn = new SqlConnection(ayarlar.cs))
        {
            SqlCommand sql = new SqlCommand("Delete from " + TabloAdi + " " + Where + " ", conn);
            conn.Open();
            sql.ExecuteNonQuery();
            sql.Dispose();
            conn.Close();
        }
    }

    public static DataTable DTGetir(string SqlCommand)
    {
        SqlConnection conn = new SqlConnection(ayarlar.cs);
        SqlCommand cmd = new SqlCommand(SqlCommand, conn);
        cmd.CommandTimeout = 0;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds,"TABLE");
            return ds.Tables["TABLE"];
        }
        catch (SqlException hata)
        {
            throw new Exception(hata.Message);
        }
        finally
        {
            if (conn != null && conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            conn = null;
        }
    }
}